<?php
/**
 * vanephp web framework
 * 简单调度器类
 *
 * @author aray <ofree@qq.com>
 * @link http://www.vanephp.com/
 * @copyright Copyright &copy; 2010 aray
 * @license http://www.vanephp.com/license
 * @package dispatcher
 */

if ( ! defined ( 'INVANE' ) )
	exit ( '!' );

/**
 * 简单调度器类 
 * @author aray
 *
 */
class vane_dispatcher_simple {
	protected $controller = null;
	protected $action = null;
	
	public function vane_dispatcher_simple ( $request ) {
		switch ( config::get ( 'url_mode' ) ) {
			case 'url_get' :
				$this->controller = isset ( $request[config::get ( 'controller_accessor' )] ) && ! empty ( $request[config::get ( 'controller_accessor' )] ) ? $request[config::get ( 'controller_accessor' )] : config::get ( 'default_controller' );
				$this->action = isset ( $request[config::get ( 'action_accessor' )] ) && ! empty ( $request[config::get ( 'action_accessor' )] ) ? $request[config::get ( 'action_accessor' )] : config::get ( 'default_action' );
				break;
			default :
				# 内部路由
				foreach ( config::get ( 'route_rule' ) as $from => $to ) {
					$from = "/{$from}/";
					$_SERVER['QUERY_STRING'] = preg_replace ( $from, $to, $_SERVER['QUERY_STRING'] );
				}
				
				$queryString = '';
				if ( isset ( $_SERVER['PATH_INFO'] ) ) {
					$queryString = substr ( $_SERVER['PATH_INFO'], 1 );
				} else {
					if ( substr ( $_SERVER['QUERY_STRING'], 0, 1 ) == '/' ) {
						$queryString = substr ( $_SERVER['QUERY_STRING'], 1 );
					} else {
						$queryString = $_SERVER['QUERY_STRING'];
					}
				}
				
				$pathinfoArr = explode ( config::get ( 'pathinfo_separator' ), $queryString );
				$this->controller = config::get ( 'default_controller' );
				if ( isset ( $pathinfoArr[0] ) && $pathinfoArr[0] != '' && $pathinfoArr[0] != null ) {
					$this->controller = $pathinfoArr[0];
				}
				$this->action = config::get ( 'default_action' );
				if ( isset ( $pathinfoArr[1] ) && $pathinfoArr[1] != '' && $pathinfoArr[1] != null ) {
					$this->action = $pathinfoArr[1];
				}
				# 将控制器和动作名称并入GET内
				$_GET = array (
					config::get ( 'controller_accessor' ) => $this->controller, config::get ( 'action_accessor' ) => $this->action 
				);
				#
				$parames = array_slice ( $pathinfoArr, 2 );
				$cunt = count ( $parames );
				# 将结果并入$_GET;
				for ( $i = 0 ; $i < $cunt ; $i += 2 ) {
					if ( isset ( $parames[$i + 1] ) ) {
						$_GET[$parames[$i]] = $parames[$i + 1];
					} else {
						$_GET[$parames[$i]] = '';
					}
				}
				# 将结果并入$_REQUEST
				unset ( $_REQUEST[$_SERVER['QUERY_STRING']] );
				$_REQUEST = array_merge ( $_REQUEST, $_GET );
				break;
		}
	}
	
	/**
	 * 验证(被vane_dispatcher_auth重写)
	 */
	public function check ( ) {
		return true;
	}
	
	/**
	 * 获取当前控制器名称
	 */
	public function getController ( ) {
		return $this->controller;
	}
	
	/**
	 * 获取当前动作名称
	 */
	public function getAction ( ) {
		return $this->action;
	}
	
	/**
	 * 验证用户是否为角色身份(被vane_dispatcher_auth重写)
	 * @param string $role		角色名称
	 */
	public function checkRole ( $role ) {
		return true;
	}
	
	/**
	 * 加载ACT文件(被vane_dispatcher_auth重写)
	 */
	private function loadACT ( ) {
		return '';
	}
}